package com.rayo.accesscontrollercopy.module.setting;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.rayo.accesscontrollercopy.R;
import com.rayo.accesscontrollercopy.model.db.bean.KeyBoxData;
import com.rayo.accesscontrollercopy.model.db.dao.KeyBoxDao;
import com.rayo.accesscontrollercopy.model.db.dao.KeyBoxImpl;
import com.rayo.accesscontrollercopy.module.adapter.KeyBoxAdapter;
import com.rayo.accesscontrollercopy.module.main.MainActivity;
import com.rayo.accesscontrollercopy.utils.PreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.rayo.accesscontrollercopy.module.main.MainActivity.UPDATA;

public class SetBoxActivity extends AppCompatActivity {
    @BindView(R.id.list_keybox)
    RecyclerView keyboxListview;
    @BindView(R.id.btn_fragment_system_ok)
    Button btnOk;

    private KeyBoxAdapter adapter;
    private List<KeyBoxData> dataList =new ArrayList<KeyBoxData>();
    private List<KeyBoxData> dataListKey;
    private   KeyBoxDao dao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_box);
        ButterKnife.bind(this);
        initData();
    }

    public void initData() {

        dao = new KeyBoxImpl();

        dataListKey = dao.findAll();

        LinearLayoutManager mlayoutManager = new LinearLayoutManager(this);
        adapter = new KeyBoxAdapter(getData(),this);
        keyboxListview.setLayoutManager(mlayoutManager);
        keyboxListview.setAdapter(adapter);

    }

    @OnClick(R.id.btn_fragment_system_ok)
    public void onClick() {
        boolean isFirst = PreferencesUtils.getBoolean(this,"isFirst");

        if (isFirst){
            KeyBoxData keyBoxData ;
            for (int i=0;i<dataList.size();i++){
                keyBoxData =  dao.findById(dataList.get(i).getKeyboxId());
                keyBoxData.setKeyboxId(dataList.get(i).getKeyboxId());
                keyBoxData.setIsopen(dataList.get(i).isIsopen());
                keyBoxData.setKeyboxName(dataList.get(i).getKeyboxName());
                dao.updataKeyBox(keyBoxData);
            }
            Intent intent = new Intent();
            intent.setAction(UPDATA);
            sendBroadcast(intent);
            Toast.makeText(this, getString(R.string.save_successful), Toast.LENGTH_SHORT).show();
            finish();
        }else {
            PreferencesUtils.putBoolean(this,"isFirst",true);
            KeyBoxData keyBoxData ;
            for (int i=0;i<dataList.size();i++){
                keyBoxData = new KeyBoxData();
                keyBoxData.setKeyboxId(dataList.get(i).getKeyboxId());
                keyBoxData.setIsopen(dataList.get(i).isIsopen());
                keyBoxData.setKeyboxName(dataList.get(i).getKeyboxName());
                dao.addKeyBox(keyBoxData);
            }
            Intent intent = new Intent();
            intent.setAction(UPDATA);
            sendBroadcast(intent);
            Toast.makeText(this, getString(R.string.save_successful), Toast.LENGTH_SHORT).show();
            finish();
        }


    }
    private List<KeyBoxData> getData(){
        if (dataListKey.size()==0){
            KeyBoxData keyBoxData ;
            for (int i=0;i<4;i++){
                keyBoxData = new KeyBoxData();
                keyBoxData.setKeyboxId((byte)(i+1));
                keyBoxData.setIsopen(false);
                keyBoxData.setKeyboxName(getString(R.string.box)+(i+1));
                dataList.add(keyBoxData);
            }
        }else {
            KeyBoxData keyBoxData ;
            for (int i=0;i<dataListKey.size();i++){
                keyBoxData = new KeyBoxData();
                keyBoxData.setKeyboxId(dataListKey.get(i).getKeyboxId());
                keyBoxData.setIsopen(dataListKey.get(i).isIsopen());
                keyBoxData.setKeyboxName(dataListKey.get(i).getKeyboxName());
                dataList.add(keyBoxData);
            }
        }

        return  dataList;
    }
}
